<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/header.css"/>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav title-bar">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left icon-arrowleft"></a>
			<a id="openMenu" class="mui-icon mui-icon-right-nav mui-pull-right icon-optionright">…</a>
			<h1 class="mui-title title-content">个人头像</h1>
		</header>
		<div class="mui-content">
			<img id="img_myface" src="image/helmos.png" >
		</div>
		<div id="sheet" class="mui-popover mui-popover-bottom mui-popover-action ">
			<!-- 可选择菜单 -->
			<ul class="mui-table-view">
			  <li class="mui-table-view-cell">
				<a id="link_choosePhoto" href="#">选择照片</a>
			  </li>
			  <li class="mui-table-view-cell">
				<a id="link_savePhoto" href="#">下载照片</a>
			  </li>
			</ul>
			<!-- 取消菜单 -->
			<ul class="mui-table-view">
			  <li class="mui-table-view-cell">
				<a href="#sheet"><b>取消</b></a>
			  </li>
			</ul>
		</div>
		
		<script src="js/mui.js"></script>
		<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			mui.init();
			
			mui.plusReady(function() {
				refreshFace();
				
				// 绑定选择菜单
				document.getElementById("openMenu").addEventListener('tap', function() {
					mui("#sheet").popover('toggle');
				});
				document.getElementById("link_choosePhoto").addEventListener('tap', function() {
					mui.openWindow({
						url: "plugin/v3.1.6/myface-uploader.html", 
						id: "myface-uploader.html",
						createNew: true
					});
					mui("#sheet").popover('toggle');
				});
				document.getElementById("link_savePhoto").addEventListener("tap", function() {
					plus.nativeUI.showWaiting("下载中...");
					// 获取当前登录用户的全局对象
					var userinfo = app.getGlobalUserInfo();
					var faceImageBig = userinfo.faceImageBig;
					var dtask = plus.downloader.createDownload(
						app.imgServerUrl + faceImageBig,
						{},
						function(d, status) {
							plus.nativeUI.closeWaiting();
							if(status == 200) {
								var tempFile = d.filename;
								// 通过相册API保存照片到本地相册
								plus.gallery.save(tempFile, function() {
									app.showToast("保存照片成功！", "success");
								});
							} else {
								app.showToast("下载错误", "error");
							}
						}
					);
					dtask.start();
				});
				
				window.addEventListener("refresh-myface", function() {
					refreshFace();
				});
			});
			
			function refreshFace() {
				// 获取当前登录用户的全局对象
				var userinfo = app.getGlobalUserInfo();
				// 如果用户全局对象不为空
				if(userinfo != null) {
					// 新用户注册没有设置头像，就用默认头像
					var face_img_src = app.default_face_img_big_src;
					
					if(app.isNotNull(userinfo.faceImageBig)) {
						face_img_src = userinfo.faceImageBig;
					}
					var img_myface = document.getElementById("img_myface");
					img_myface.src = app.imgServerUrl + face_img_src;
					// 获取手机端可见屏幕的宽度
					var imgWidth = document.body.clientWidth;
					img_myface.width = imgWidth;
					img_myface.height = imgWidth;
				}
			}
		</script>
	</body>

</html>
